/**
 * Created by SylarSong on 16/12/7.
 */



//************获取上一个月***************
function getPreMonth(date) {
    /**
     * @param: data    例如  new Date()
     * @param: return  时间戳  例如  new Date().getTime()
     * **/
    if (!date)date = new Date();
    var year = date.getFullYear(); //获取当前日期的年份
    var month = date.getMonth(); //获取当前日期的月份
    var day = date.getDate(); //获取当前日期的日
    var oneDay = 1000 * 3600 * 24 * 1;
    var days = new Date(year, month, 0).getDate(); //获取当前日期中月的天数
    var current_time = date.getTime();
    var after_time = current_time - (oneDay * days);
    return after_time;
}
//************获取当前以前N天日期信息***************
function getPerDay(cycle) {
    var res = [];
    var oneDay = 1000 * 3600 * 24 * 1;
    var now = new Date().getTime();
    for (var i = 0; i < cycle; i++) {
        res.push([new Date(now).getMonth() + 1, new Date(now).getDate()].join("-"))
        now -= oneDay;
    }
    return res.reverse()
}

//************制作N个假数据***************
function getRandomData(n, f) {
    var res = [];
    for (var i = 0; i < n; i++) {
        res.push(Math.floor(Math.random() * f));
    }
    return res;
}
//************获取图标实例******************
var line_bar_newPay_allPay = echarts.init($('#line-bar-newPay-allPay')[0]); // 新增充值用户数,累计充值用户数概况
var line_chart_recently_money = echarts.init($('#line-chart-recently-money')[0]);
var histogram_chart_payFrequency_payPlayers = echarts.init($('#histogram-chart-payFrequency-payPlayers')[0]);
var recharge_count_data;  // 充值次数,充值人数对比图
var first_recharge_count_data;  // 新增充值用户数
var income_trend_data;  // 近期收入走势
//!!!!!!!!!!!!!!!准备时间!!!!!!!!!!!!!
$(document).ready(function () {

    $.get('/stats_ajax/recharge/count', {'_': $.now(), 'recharge_count_date': recharge_count_date}, function(res) {
        recharge_count_data = res;
        var data={
            date : res['months'],
            data1 : res['frequency'],
            data2 : res['people_num']
        };
        get_payFrequency_payPlayers(histogram_chart_payFrequency_payPlayers, data);
    });

    $.get('/stats_ajax/first_recharge_count', {'_': $.now(), 'first_recharge_count_date': first_recharge_count_date}, function(res){
        first_recharge_count_data = res;
        var data = {
            date: res['months'],
            data1: res['news'],
            data2: res['totals']
        };
        get_newPay_allPay(line_bar_newPay_allPay, data);
    });

    $.get('/stats_ajax/income_trend_count', {'_': $.now(), 'income_trend_date': income_trend_date}, function(res){
        income_trend_data = res;
        get_recently_money(line_chart_recently_money, res);
    });


    // recharge_count_data['months'].slice(-7, recharge_count_data['months'].length),
    //新增充值用户数,累计充值用户数概况!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //日按钮时间监听
    $('#btn-newPay-allPay-day').click(function () {
        line_bar_newPay_allPay = echarts.init($('#line-bar-newPay-allPay')[0]);
        line_bar_newPay_allPay.showLoading();
        var data = {
            data1: first_recharge_count_data['news'][first_recharge_count_data['news'].length - 1],
            data2 : first_recharge_count_data['totals'][first_recharge_count_data['totals'].length - 1]
        };
        get_newPay_allPay_day(line_bar_newPay_allPay, data);
    });
    //周按钮时间监听
    $('#btn-newPay-allPay-week').click(function () {
        line_bar_newPay_allPay.showLoading();
        var data = {
            data1: first_recharge_count_data['news'].slice(-7, first_recharge_count_data.length),
            data2: first_recharge_count_data['totals'].slice(-7, first_recharge_count_data.length),
            date: first_recharge_count_data['months'].slice(-7, first_recharge_count_data.length)
        };
        get_newPay_allPay(line_bar_newPay_allPay, data);
    });
    //月按钮时间监听
    $('#btn-newPay-allPay-month').click(function () {
        line_bar_newPay_allPay.showLoading();
        var data = {
            data1: first_recharge_count_data['news'],
            data2: first_recharge_count_data['totals'],
            date: first_recharge_count_data['months']
        };
        get_newPay_allPay(line_bar_newPay_allPay, data);
    });
    //近期收入走势图!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //日按钮时间监听
    $('#btn-recently-day').click(function () {
        line_chart_recently_money= echarts.init($('#line-chart-recently-money')[0]);
        line_chart_recently_money.showLoading();
        // TODO 目前还没有表暂时 percentage 填1
        var data={
            name:'当日收入额', // 数据名称
            money: income_trend_data['data'][income_trend_data['data'].length - 1],    //当日实收 RMB
            percentage:1   // 达成目标百分比
        };
        get_recently_money_day(line_chart_recently_money,data);
    });
    //周按钮时间监听
    $('#btn-recently-week').click(function () {
        line_chart_recently_money.showLoading();
        var data={
            data: income_trend_data['data'].slice(-7, income_trend_data.length),
            date: income_trend_data['date'].slice(-7, income_trend_data.length)
        };

        get_recently_money(line_chart_recently_money, data);
    });
    //月按钮时间监听
    $('#btn-recently-month').click(function () {
        line_chart_recently_money.showLoading();
        var data={
            data: income_trend_data['data'],
            date: income_trend_data['date']
        };
        get_recently_money(line_chart_recently_money, data);
    });
    //充值次数,充值人数对比图!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //日按钮时间监听


    $('#btn-payFrequency-payPlayers-day').click(function () {

        histogram_chart_payFrequency_payPlayers.showLoading();
        var data={
            date : recharge_count_data['months'].slice(-1, recharge_count_data['months'].length),
            data1 : recharge_count_data['months'].slice(-1, recharge_count_data['frequency'].length),
            data2 : recharge_count_data['months'].slice(-1, recharge_count_data['people_num'].length)
        };
        get_payFrequency_payPlayers(histogram_chart_payFrequency_payPlayers,data);
    });
    //周按钮时间监听
    $('#btn-payFrequency-payPlayers-week').click(function () {
        histogram_chart_payFrequency_payPlayers.showLoading();
        var data={
            date : recharge_count_data['months'].slice(-7, recharge_count_data['months'].length),
            data1 : recharge_count_data['months'].slice(-7, recharge_count_data['frequency'].length),
            data2 : recharge_count_data['months'].slice(-7, recharge_count_data['people_num'].length)
        };
        get_payFrequency_payPlayers(histogram_chart_payFrequency_payPlayers, data);
    });
    //月按钮时间监听
    $('#btn-payFrequency-payPlayers-month').click(function () {
        histogram_chart_payFrequency_payPlayers.showLoading();
        var data={
            date : recharge_count_data['months'],
            data1 : recharge_count_data['frequency'],
            data2 : recharge_count_data['people_num']
        };
        get_payFrequency_payPlayers(histogram_chart_payFrequency_payPlayers, data);
    });


});

//!!!!!!!!改变尺寸触发!!!!!!!!!!!!
window.onresize = function () {
    $('#line-bar-newPay-allPay').width($(".grid-12") - 50);
    line_bar_newPay_allPay.resize();
};

//*新增充值用户数,累计充值用户数概况********************************************
function get_newPay_allPay(instance, data) {
    /***
     * @param: instance  echart 实例 例如: echart.init($(******)[0]) 获取
     * @param: date  x周时间信息;
     * @param: data1 数据1 这里新增付费用户数量
     * @param: data2 数据2 这里累计付费用户数量
     * @cycle: 查询周期 , 1,7,30 天
     */


    var option = {
        title: {
            text: '新增充值用户数,累计充值用户数概况',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c}'
        },
        legend: {
            left: 'left',
            data: ['新增充值用户数', '累计充值用户数']
        },
        xAxis: {
            type: 'category',
            name: 'x',
            splitLine: {show: false},
            data: data.date
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        yAxis: {},
        series: [
            {
                name: '新增充值用户数',
                type: 'line',
                data: data.data1
            },
            {
                name: '累计充值用户数',
                type: 'line',
                data: data.data2
            }
        ]
    };
    instance.setOption(option);

    setTimeout(function () {
        instance.hideLoading();
    }, 500);
}


//*新增充值用户数,累计充值用户当日数据********************************************
function get_newPay_allPay_day(instance, data) {
    /**
     * @param instance echarts初始化的实例, 例如: echarts.init($('#line-bar-newPay-allPay')[0]);
     * @param data1 数据1 新增充值用户
     * @param data2 数据2 原有充值用户
     * **/
    if (!data){
        data = {
            data1:30,
            data2 : 70
        }
    }


    option = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data: ['累计充值用户', '新增充值用户', '原充值用户']
        },
        series: [
            {
                name: '访问来源',
                type: 'pie',
                selectedMode: 'single',
                radius: [0, '30%'],

                label: {
                    normal: {
                        position: 'inner'
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data: [
                    {value: data.data1 + data.data2, name: '累计充值用户'}
                ]
            },
            {
                name: '当日数据',
                type: 'pie',
                radius: ['40%', '55%'],

                data: [
                    {value: data.data1, name: '新增充值用户'},
                    {value: data.data2, name: '原充值用户'}

                ]
            }
        ]
    };
    instance.setOption(option);
    setTimeout(function () {
        instance.hideLoading();
    }, 500);
}
//*近期收入走势********************************************
function get_recently_money(instance, data) {
    if (!data) {
        data={
            data:getRandomData(7,10000), // 数据
            date:getPerDay(7),    // 日期
        }
    }
    option = {
        title: {
            text: '近期收入走势图'
        },
        tooltip: {
            trigger: 'axis',
            formatter: "{a} <br/>{b}: {c} 元"
        },
        legend: {
            data: ['收入']
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: false,
                data: data.date   //X周日期
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '收入',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: data.data    //实际数据
            }
        ]
    };
    instance.setOption(option);
    setTimeout(function () {
        instance.hideLoading();
    }, 500);
}

function get_recently_money_day(instance, data) {
    //# 数据结构案例
    if(!data){
        data={
            name:'当日收入额',
            percentage:[{
                name: '11111',
                value: 1
            }, 0.95, 0.9, 0.85]
        }
    }
    if(!data.name){alert("数据缺少 name ");return}
    if(!data.percentage){alert("数据缺少 percentage ");return}

    var data1 = {
        name:data.name,
        percentage:[{
            name:data.money,
            value:data.percentage
        },(data.percentage-0.05),(data.percentage-0.1),(data.percentage-0.15)]
    }

    var option = {
        series: [{
            type: 'liquidFill',
            name: data1.name,
            data: data1.percentage,
            shape: 'roundRect',
            radius: '90%',
            label: {
                normal: {
                    formatter: function(param) {
                    return param.seriesName + '\n'
                        + param.name + '\n'
                        + '完成度:' + param.value*100+'%';
                },
                    textStyle: {
                        fontSize: 28
                    }
                }
            },
            backgroundStyle: {
                shadowColor: 'rgba(0, 0, 0, 0.4)',
                shadowBlur: 30
            },
            outline: {
                show: false
            },
        }]
    };
    instance.setOption(option);
    setTimeout(function () {
        instance.hideLoading();
    }, 500);
}

//************充值次数,充值人数对比图概况******************
function get_payFrequency_payPlayers(instance, data) {
    /**
     * @param instance echarts初始化的实例, 例如: echarts.init($('#line-bar-newPay-allPay')[0]);
     * @param date 时间信息 X轴数据
     * @param data1 数据1 充值人数
     * @param data2 数据2 充值次数
     * **/


    option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            data: ['充值次数', '充值人数']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        yAxis: [
            {
                type: 'value'
            }
        ],
        xAxis: [
            {
                type: 'category',
                axisTick: {show: false},
                data: data.date
            }
        ],
        series: [
            {
                name: '充值次数',
                type: 'bar',
                label: {
                    normal: {
                        show: true,
                        position: 'inside'
                    }
                },
                data: data.data1
            },
            {
                name: '充值人数',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true
                    }
                },
                data: data.data2
            }
        ]
    };
    instance.setOption(option);
    setTimeout(function () {
        instance.hideLoading();
    }, 500);
}